package org.jeecg.modules.pipe.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.pipe.entity.PumpStation;
import org.jeecg.modules.pipe.entity.Section;
import org.jeecg.modules.pipe.entity.WaterPlant;
import org.jeecg.modules.pipe.mapper.MonitorPointMapper;
import org.jeecg.modules.pipe.mapper.PumpStationMapper;
import org.jeecg.modules.pipe.mapper.SectionMapper;
import org.jeecg.modules.pipe.mapper.WaterPlantMapper;
import org.jeecg.modules.pipe.service.ISectionService;
import org.jeecg.modules.pipe.vo.PipeMarkerVo;
import org.jeecg.modules.pipe.vo.PipeQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 管网管段
 * @Author: jeecg-boot
 * @Date:   2024-05-14
 * @Version: V1.0
 */
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section> implements ISectionService {

    @Autowired
    private WaterPlantMapper waterPlantMapper;
    @Autowired
    private PumpStationMapper pumpStationMapper;
    @Autowired
    private SectionMapper sectionMapper;
    @Autowired
    private MonitorPointMapper monitorPointMapper;

    @Override
    public void saveSection(Section section) {
        if(null != section.getPumpId() && section.getPumpId()>0 ){
            if(StringUtils.isEmpty(section.getBeginLat())){
                PumpStation pumpStation = pumpStationMapper.selectById(section.getPumpId());
                section.setBeginLong(pumpStation.getLongitude());
                section.setBeginLat(pumpStation.getLatitude());
            }
        }else if(null != section.getPlantId() && section.getPlantId()>0 ){
            if(StringUtils.isEmpty(section.getBeginLat())){
                WaterPlant waterPlant = waterPlantMapper.selectById(section.getPlantId());
                section.setBeginLong(waterPlant.getLongitude());
                section.setBeginLat(waterPlant.getLatitude());
            }
        }

        if(null != section.getParentId() && section.getParentId()>0 ){
            if(StringUtils.isEmpty(section.getBeginLat())){
                Section parentSection = sectionMapper.selectById(section.getParentId());
                section.setBeginLong(parentSection.getEndLong());
                section.setBeginLat(parentSection.getEndLat());
            }
        }

        if(null != section.getId() && section.getId()>0 ){
            this.updateById(section);
        }else{
            this.save(section);
        }
    }

    /**
     * 获取管段Marker
     * @param queryVo
     * @return
     */
    @Override
    public List<PipeMarkerVo> getSectionMarker(PipeQueryVo queryVo) {
        return sectionMapper.getSectionMarker(queryVo);
    }

    /**
     * 获取管网Marker
     *  -水厂
     *  -泵站
     *  -管段
     *  -监测点
     * @param queryVo
     * @return
     */
    @Override
    public List<PipeMarkerVo> getPipeMarker(PipeQueryVo queryVo) {
        List<PipeMarkerVo> markerLs = new ArrayList();
        List<PipeMarkerVo> markerVoList = waterPlantMapper.getWaterPlantMarker(queryVo);
        if(null != markerVoList && markerVoList.size() > 0){
            markerLs.addAll(markerVoList);
        }

        markerVoList = pumpStationMapper.getPumpStationMarker(queryVo);
        if(null != markerVoList && markerVoList.size() > 0){
            markerLs.addAll(markerVoList);
        }
        markerVoList = sectionMapper.getSectionMarker(queryVo);
        if(null != markerVoList && markerVoList.size() > 0){
            markerLs.addAll(markerVoList);
        }
        markerVoList = monitorPointMapper.getMonitorPointMarker(queryVo);
        if(null != markerVoList && markerVoList.size() > 0){
            markerLs.addAll(markerVoList);
        }
        return markerLs;
    }
}
